package user_detail

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/middleware"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

func (b Base) UpdateUserDetail(c *gin.Context, req *model.UserDetail) *UpdateUserDetailResponse {
	detail := &model.UserDetail{}
	detail.UserId = middleware.GetUserId(c)
	// 查找出来
	err := detail.GetByUserId(config.DB)
	if err != nil {
		handler.Errorf(c, "记录不存在")
		return nil
	}

	// 按照传过来的条件进行修改
	if req.PatientNumber != "" {
		detail.PatientNumber = req.PatientNumber
	}
	if req.Weight != 0 {
		detail.Weight = req.Weight
	}
	if req.Height != 0 {
		detail.Height = req.Height
	}
	if req.Gender != 0 {
		detail.Gender = req.Gender
	}

	if req.Age != 0 {
		detail.Age = req.Age
	}

	if req.SmokingHistory != 0 {
		detail.SmokingHistory = req.SmokingHistory
	}

	if req.AlcoholDrinkingHistory != "" {
		detail.AlcoholDrinkingHistory = req.AlcoholDrinkingHistory
	}

	if req.TypeOfDiabetes != "" {
		detail.TypeOfDiabetes = req.TypeOfDiabetes
	}

	if req.DurationOfDiabetes != 0 {
		detail.DurationOfDiabetes = req.DurationOfDiabetes
	}

	if req.AcuteDiabeticComplications != "" {
		detail.AcuteDiabeticComplications = req.AcuteDiabeticComplications
	}

	if req.DiabeticMacrovascularComplications != "" {
		detail.DiabeticMacrovascularComplications = req.DiabeticMacrovascularComplications
	}

	if req.DiabeticMicrovascularComplications != "" {
		detail.DiabeticMicrovascularComplications = req.DiabeticMicrovascularComplications
	}

	if req.Comorbidities != "" {
		detail.Comorbidities = req.Comorbidities
	}

	if req.HypoglycemicAgents != "" {
		detail.HypoglycemicAgents = req.HypoglycemicAgents
	}

	if req.OtherAgents != "" {
		detail.OtherAgents = req.OtherAgents
	}

	if req.FastingPlasmaGlucose != 0 {
		detail.FastingPlasmaGlucose = req.FastingPlasmaGlucose
	}

	if req.TwoHourPostprandialPlasmaGlucose != 0 {
		detail.TwoHourPostprandialPlasmaGlucose = req.TwoHourPostprandialPlasmaGlucose
	}

	if req.FastingCPeptide != 0 {
		detail.FastingCPeptide = req.FastingCPeptide
	}

	if req.TwoHourPostprandialCPeptide != 0 {
		detail.TwoHourPostprandialCPeptide = req.TwoHourPostprandialCPeptide
	}

	if req.FastingInsulin != 0 {
		detail.FastingInsulin = req.FastingInsulin
	}
	if req.TwoHourPostprandialInsulin != 0 {
		detail.TwoHourPostprandialInsulin = req.TwoHourPostprandialInsulin
	}
	if req.Hba1c != 0 {
		detail.Hba1c = req.Hba1c
	}
	if req.GlycatedAlbumin != 0 {
		detail.GlycatedAlbumin = req.GlycatedAlbumin
	}
	if req.TotalCholesterol != 0 {
		detail.TotalCholesterol = req.TotalCholesterol
	}
	if req.Triglyceride != 0 {
		detail.Triglyceride = req.Triglyceride
	}
	if req.HighDensityLipoproteinCholesterol != 0 {
		detail.HighDensityLipoproteinCholesterol = req.HighDensityLipoproteinCholesterol
	}
	if req.LowDensityLipoproteinCholesterol != 0 {
		detail.LowDensityLipoproteinCholesterol = req.LowDensityLipoproteinCholesterol
	}
	if req.Creatinine != 0 {
		detail.Creatinine = req.Creatinine
	}
	if req.EstimatedGlomerularFiltrationRate != 0 {
		detail.EstimatedGlomerularFiltrationRate = req.EstimatedGlomerularFiltrationRate
	}
	if req.UricAcid != 0 {
		detail.UricAcid = req.UricAcid
	}
	if req.BloodUreaNitrogen != 0 {
		detail.BloodUreaNitrogen = req.BloodUreaNitrogen
	}
	if req.Hypoglycemia != 0 {
		detail.Hypoglycemia = req.Hypoglycemia
	}

	// 修改完成后保存
	err = detail.Update(config.DB)
	if err != nil {
		handler.Errorf(c, "更新记录失败")
		log.Error(err)
		return nil
	}
	return &UpdateUserDetailResponse{}
}

type UpdateUserDetailResponse struct {
}
